﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace ProjectNocturnalFool
{
    class PartySelectControl : UserControl
    {
        private TextboxControl textBoxCtrl;
        private PartyInfoControl partyInfoCtrl;


        #region Fields
        public override int width
        {
            get
            {
                return _width;
            }
            set
            {
                _width = value;
                float spacing = width - (GameLogic.Instance.gameCharacterSession.maxStartCharacters * Constants.PartyMemberWidth);
                spacing = spacing / (GameLogic.Instance.gameCharacterSession.maxStartCharacters + 1);

                for (int i = 0; i < GameLogic.Instance.gameCharacterSession.maxStartCharacters; i++)
                {
                    controls[i].x = x + ((int)spacing * (i + 1)) + (Constants.PartyMemberWidth * i);
                }

                partyInfoCtrl.width = width - Constants.ControlSmallOffsets * 2;
            }
        }
        
        #endregion 

        #region Override Sections
        /// <summary>
        /// Loads Content of party select control, reload content if campaign parameters change
        /// </summary>
        public override void LoadContent()
        {
            base.LoadContent();

            _bgTexture = TacticsGame.Instance.Content.Load<Texture2D>(Constants.TextureWhite);
            
            depth = ControlDepth.NonDrag;
            
            for (int i = 0; i < GameLogic.Instance.gameCharacterSession.maxStartCharacters; i++)
            {
                PartyPortraitControl partyMemberCtrl = new PartyPortraitControl();
                partyMemberCtrl.LoadContent();
                partyMemberCtrl.characterSlot = i;
                partyMemberCtrl.y = y + Constants.ScreenBorder + Constants.ControlOffsets + Constants.ControlSmallOffsets;
                controls.Add(partyMemberCtrl);
            }
            LabelControl partyLbl = UILoader.addMessageLabel(x + Constants.ControlSmallOffsets, y + Constants.ControlSmallOffsets , "Party Name: ");
            controls.Add(partyLbl);

            textBoxCtrl = UILoader.addTextBox(x + partyLbl.width, y +Constants.ControlSmallOffsets);
            textBoxCtrl.maxInput = 16;
            textBoxCtrl.width = 100;
            controls.Add(textBoxCtrl);

            partyInfoCtrl = new PartyInfoControl();
            partyInfoCtrl.LoadContent();
            partyInfoCtrl.x = Constants.ControlSmallOffsets;
            partyInfoCtrl.y = y + Constants.ScreenBorder + Constants.ControlOffsets + Constants.ControlOffsets + Constants.PartyMemberHeight;
            partyInfoCtrl.height = VirtualScreen.Instance.VirtualHeight - (Constants.ScreenBorder * 2) - Constants.ButtonHeight - Constants.ControlOffsets -(y + Constants.ScreenBorder + Constants.ControlOffsets + Constants.ControlOffsets + Constants.PartyMemberHeight + Constants.ControlSmallOffsets);
            controls.Add(partyInfoCtrl);

        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if (HoverControl != null)
            {
                if (HoverControl is PartyPortraitControl)
                {
                    PartyPortraitControl ctrl = (PartyPortraitControl)HoverControl;
                    partyInfoCtrl.displayChar = ctrl.characterSlot;
                }
            }
            else
            {
                partyInfoCtrl.displayChar = Constants.INDEX_INVALID;
            }
            

        }

        /// <summary>
        /// Draws the entire party select control
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Draw(GameTime gameTime)
        {

            TacticsGame.Instance.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone);

            Rectangle targetRect = new Rectangle(x, y, width, height);

            TacticsGame.Instance.spriteBatch.Draw(_bgTexture,
                                                      targetRect,
                                                      null,
                                                      Color.White * 0.8f,
                                                      0,
                                                      Vector2.Zero,
                                                      SpriteEffects.None,
                                                      0);


            TacticsGame.Instance.spriteBatch.End();


            base.Draw(gameTime);
        }

        #endregion

    }
}
